<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <div th:replace="./widget/base"></div>
</head>
<body>

<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'west',split:true" style="width:30%">
        <table id="roleAuthorityViewPageCategory"></table>
    </div>
    <div data-options="region:'center',split:true">
        <div id="roleAuthorityViewPageToolbar">
            <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/USER/ROLE_AUTHORITY/VIEW_PAGE/PAGE_ALL/GRANT_ALL_ROLE_AUTHORITY_VIEW_PAGE'')')}"
               href="javascript:" class="easyui-linkbutton" iconCls="fa fa-plus" plain="true"
               onclick="grantAllRoleAuthorityViewPage();">授权</a>

            <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/USER/ROLE_AUTHORITY/VIEW_PAGE/PAGE_ALL/REVOKE_ALL_ROLE_AUTHORITY_VIEW_PAGE'')')}"
               href="javascript:" class="easyui-linkbutton" iconCls="fa fa-close" plain="true"
               onclick="revokeAllRoleAuthorityViewPage();">撤销授权</a>
        </div>
        <table id="roleAuthorityViewPage"></table>
    </div>
    <div data-options="region:'east',split:true" style="width:30%">
        <div id="roleAuthorityViewPageComponentToolbar">
            <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/USER/ROLE_AUTHORITY/VIEW_PAGE/PAGE_ALL/GRANT_ALL_ROLE_AUTHORITY_VIEW_PAGE_COMPONENT'')')}"
               href="javascript:" class="easyui-linkbutton" iconCls="fa fa-plus" plain="true"
               onclick="grantAllRoleAuthorityViewPageComponent();">授权</a>

            <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/USER/ROLE_AUTHORITY/VIEW_PAGE/PAGE_ALL/REVOKE_ALL_ROLE_AUTHORITY_VIEW_PAGE_COMPONENT'')')}"
               href="javascript:" class="easyui-linkbutton" iconCls="fa fa-close" plain="true"
               onclick="revokeAllRoleAuthorityViewPageComponent();">撤销授权</a>
        </div>
        <table id="roleAuthorityViewPageComponent"></table>

    </div>
</div>

<script type="text/javascript">
    var roleId;
    $(function () {
        var selectedRole = $('#role').datagrid('getSelected');
        if (selectedRole) {
            roleId = selectedRole.id;
        }

        var columns = [
            {field: 'checkbox', checkbox: true},
            {field: 'id', title: 'ID'},
            {field: 'name', title: '分类名称'},
            {field: 'sort', title: '排序'},
            {field: 'remark', title: '备注'},
            {field: 'gmtModified', title: '更新时间'},
            {field: 'gmtCreated', title: '创建时间'}
        ];
        $('#roleAuthorityViewPageCategory').treegrid(expressui.grid.create, {
            title: '视图页面分类',
            url: Url.System.Api.VIEW_PAGE_CATEGORY + '/page_all_parent',
            expandUrl: Url.System.Api.VIEW_PAGE_CATEGORY + '/list_all_by_parent_id/{id}',
            columns: [columns],
            onCheck: function (row) {
                if (row) {
                    $('#roleAuthorityViewPage').datagrid('reload', Url.System.Api.VIEW_PAGE + '/page_all_by_page_category_id/' + row.id);
                }
            },
            onUncheck: function (index, row) {
                $('#roleAuthorityViewPage').datagrid('loadData', []);
            }

        });

        columns = [
            {field: 'checkbox', checkbox: true},
            {
                field: 'is_granted', title: '授权状态', formatter: function (value, row) {
                    var isGranted = '未授权';
                    expressui.ajax({
                        url: Url.User.Api.ROLE_AUTHORITY + '/is_granted_by_role_id_and_authority/?roleId=' + roleId + '&authority=' + row.authority,
                        success: function (data) {
                            if (data && (data.isGranted === true)) {
                                isGranted = '已授权';
                            }
                        },
                        async: false
                    });

                    return isGranted;
                }
            },
            {field: 'id', title: 'ID'},
            {field: 'name', title: '页面名称'},
            {field: 'url', title: '请求地址（url）'},
            {field: 'authority', title: '权限（authority）'},
            {field: 'sort', title: '排序'},
            {field: 'remark', title: '备注'},
            {field: 'gmtModified', title: '更新时间'},
            {field: 'gmtCreated', title: '创建时间'}
        ];
        $('#roleAuthorityViewPage').datagrid(expressui.grid.create, {
            title: '视图页面',
            columns: [columns],
            onCheck: function (index, row) {
                if (row) {
                    $('#roleAuthorityViewPageComponent').datagrid('reload', Url.System.Api.VIEW_PAGE_COMPONENT + '/page_all_by_page_id/' + row.id);
                }
            },
            onUncheck: function (index, row) {
                $('#roleAuthorityViewPageComponent').datagrid('loadData', []);
            }
        });

        columns = [
            {field: 'checkbox', checkbox: true},
            {
                field: 'is_granted', title: '授权状态', formatter: function (value, row) {
                    var isGranted = '未授权';
                    expressui.ajax({
                        url: Url.User.Api.ROLE_AUTHORITY + '/is_granted_by_role_id_and_authority',
                        data: {
                            roleId: roleId,
                            authority: row.authority
                        },
                        async: false,
                        success: function (data) {
                            if (data && (data.isGranted === true)) {
                                isGranted = '已授权';
                            }
                        },
                    });

                    return isGranted;
                }
            },
            {field: 'id', title: 'ID'},
            {field: 'name', title: '页面名称'},
            {field: 'url', title: '请求地址（url）'},
            {field: 'authority', title: '权限（authority）'},
            {field: 'sort', title: '排序'},
            {field: 'remark', title: '备注'},
            {field: 'gmtCreated', title: '创建时间'},
            {field: 'gmtModified', title: '更新时间'}
        ];
        $('#roleAuthorityViewPageComponent').datagrid(expressui.grid.create, {
            title: '视图页面组件',
            columns: [columns]
        });
    });

    function grantAllRoleAuthorityViewPage() {
        var selectAll = $('#roleAuthorityViewPage').datagrid(expressui.grid.getCheckedLessOneOrShowAlert);
        var authorityList = [];
        if (selectAll) {
            for (var i = 0; i < selectAll.length; i++) {
                authorityList[i] = selectAll[i].authority;
            }
            expressui.ajax({
                url: Url.User.Api.ROLE_AUTHORITY + '/grant_all_by_role_id_and_page_authority_list',
                data: {roleId: roleId, authorityList: authorityList},
                type: 'post',
                success: function (data) {
                    $.messager.show({title: '信息', msg: '授权成功'});
                    $('#roleAuthorityViewPage').datagrid('reload');
                    $('#roleAuthorityViewPage').datagrid('clearSelections');
                    $('#roleAuthorityViewPage').datagrid('clearChecked');

                    $('#roleAuthorityViewPageComponent').datagrid('reload');
                    $('#roleAuthorityViewPageComponent').datagrid('clearSelections');
                    $('#roleAuthorityViewPageComponent').datagrid('clearChecked');
                },
                error: function (XMLHttpRequest) {
                    $.messager.show({title: '信息', msg: XMLHttpRequest.responseJSON.message});
                }
            })
        }
    }

    function revokeAllRoleAuthorityViewPage() {
        // $('#role').datagrid( expressui.grid.postCheckedLessOneOrShowAlert,  {url: Url.User.Api.ROLE + '/delete_all', method:'post', success: function () {
        //         $('#role').datagrid('reload');
        //     } });

        var selectAll = $('#roleAuthorityViewPage').datagrid(expressui.grid.getCheckedLessOneOrShowAlert);
        var authorityList = [];
        if (selectAll) {
            for (var i = 0; i < selectAll.length; i++) {
                authorityList[i] = selectAll[i].authority;
            }
            expressui.ajax({
                url: Url.User.Api.ROLE_AUTHORITY + '/revoke_all_by_role_id_and_page_authority_list',
                data: {roleId: roleId, authorityList: authorityList},
                type: 'post',
                success: function (data) {
                    $.messager.show({title: '信息', msg: '撤销授权成功'});
                    $('#roleAuthorityViewPage').datagrid('reload');
                    $('#roleAuthorityViewPage').datagrid('clearSelections');
                    $('#roleAuthorityViewPage').datagrid('clearChecked');

                    $('#roleAuthorityViewPageComponent').datagrid('reload');
                    $('#roleAuthorityViewPageComponent').datagrid('clearSelections');
                    $('#roleAuthorityViewPageComponent').datagrid('clearChecked');
                },
                error: function (XMLHttpRequest) {
                    $.messager.show({title: '信息', msg: XMLHttpRequest.responseJSON.message});
                }
            })
        }
    }

    function grantAllRoleAuthorityViewPageComponent() {
        var selectAll = $('#roleAuthorityViewPageComponent').datagrid(expressui.grid.getCheckedLessOneOrShowAlert);
        var authorityList = [];
        if (selectAll) {
            for (var i = 0; i < selectAll.length; i++) {
                authorityList[i] = selectAll[i].authority;
            }
            expressui.ajax({
                url: Url.User.Api.ROLE_AUTHORITY + '/grant_all_by_role_id_and_page_component_authority_list',
                data: {roleId: roleId, authorityList: authorityList},
                type: 'post',
                success: function (data) {
                    $.messager.show({title: '信息', msg: '授权成功'});
                    $('#roleAuthorityViewPageComponent').datagrid('reload');
                    // $('#roleAuthorityViewPageComponent').datagrid('clearSelections');
                    // $('#roleAuthorityViewPageComponent').datagrid('clearChecked');
                },
                error: function (XMLHttpRequest) {
                    $.messager.show({title: '信息', msg: XMLHttpRequest.responseJSON.message});
                }
            })
        }
    }

    function revokeAllRoleAuthorityViewPageComponent() {
        // $('#role').datagrid( expressui.grid.postCheckedLessOneOrShowAlert,  {url: Url.User.Api.ROLE + '/delete_all', method:'post', success: function () {
        //         $('#role').datagrid('reload');
        //     } });

        var selectAll = $('#roleAuthorityViewPageComponent').datagrid(expressui.grid.getCheckedLessOneOrShowAlert);
        var authorityList = [];
        if (selectAll) {
            for (var i = 0; i < selectAll.length; i++) {
                authorityList[i] = selectAll[i].authority;
            }
            expressui.ajax({
                url: Url.User.Api.ROLE_AUTHORITY + '/revoke_all_by_role_id_and_page_component_authority_list',
                data: {roleId: roleId, authorityList: authorityList},
                type: 'post',
                success: function (data) {
                    $.messager.show({title: '信息', msg: '撤销授权成功'});
                    $('#roleAuthorityViewPageComponent').datagrid('reload');
                    // $('#roleAuthorityViewPageComponent').datagrid('clearSelections');
                    // $('#roleAuthorityViewPageComponent').datagrid('clearChecked');
                },
                error: function (XMLHttpRequest) {
                    $.messager.show({title: '信息', msg: XMLHttpRequest.responseJSON.message});
                }
            })
        }
    }
</script>
</body>
</html>